#import "PluginImagePlugin.h"
#import "ImageManager.h"

@implementation PluginImagePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"plugin_image" binaryMessenger:[registrar messenger]];
    PluginImagePlugin* instance = [[PluginImagePlugin alloc] init];
    [registrar addMethodCallDelegate:instance channel:channel];

    FlutterEventChannel *eventChannel = [FlutterEventChannel eventChannelWithName:@"plugin_camera_event" binaryMessenger:[registrar messenger]];
    [eventChannel setStreamHandler:[PluginImagePlugin sharedInstance]];
}

+ (instancetype)sharedInstance {
    static PluginImagePlugin *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
    // 实现监听逻辑
    self.eventSink = eventSink;
    return nil;
}

- (FlutterError*)onCancelWithArguments:(id)arguments {
    // 实现取消监听逻辑
    self.eventSink = nil;
    return nil;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if ([@"openCamera" isEqualToString:call.method]) {
      [self openCamera];
  } else if ([@"openPhotoAlbum" isEqualToString:call.method]) {
      [self openPhotoAlbum:result];
  }else {
    result(FlutterMethodNotImplemented);
  }
}

- (void)openCamera{
    [[ImageManager sharedInstance] openCamera];
}

- (void)openPhotoAlbum:(FlutterResult)result {
    [[ImageManager sharedInstance] openPhotoAlbum:result];
}

@end
